//
//  MUPSandbox.m
//  MUPFoundation
//
//  Created by chen jinqiang on 14-10-14.
//  Copyright (c) 2014年 chen jinqiang. All rights reserved.
//

#import "TYSandbox.h"

static NSString *appPath = nil;
static NSString *docPath = nil;
static NSString *docInboxPath = nil;
static NSString *libPath = nil;
static NSString *libPrefPath = nil;
static NSString *libCachePath = nil;
static NSString *tmpPath = nil;

#pragma mark -

@interface TYSandbox()
@property (nonatomic, copy) NSString *appPath;
@property (nonatomic, copy) NSString *docPath;
@property (nonatomic, copy) NSString *docInboxPath;
@property (nonatomic, copy) NSString *libPath;
@property (nonatomic, copy) NSString *libPrefPath;
@property (nonatomic, copy) NSString *libCachePath;
@property (nonatomic, copy) NSString *tmpPath;
@property (nonatomic, copy) NSString *domain;
@property (nonatomic, copy) NSString *uniqueUserName;
@end

#pragma mark -

@implementation TYSandbox

IMP_SHAREDINSTANCE(TYSandbox);

- (instancetype)init {
    self = [super init];
    if (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            appPath = [[NSBundle mainBundle] bundlePath];
            docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
            docInboxPath = [docPath stringByAppendingPathComponent:@"Inbox"];
            libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
            libPrefPath = [docPath stringByAppendingPathComponent:@"Preferences"];
            libCachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
            tmpPath = NSTemporaryDirectory();
        });
    }
    return self;
}

- (NSString *)appPath {
    if (!_appPath ) {
        _appPath = [self domainPathWithOriginPath:appPath];
    }
    
    return _appPath;
}

- (NSString *)docPath {
    if (!_docPath ) {
        _docPath = [self domainPathWithOriginPath:docPath];
    }
    
    return _docPath;
}

- (NSString *)docInboxPath {
    if (!_docInboxPath ) {
        _docInboxPath = [self domainPathWithOriginPath:docInboxPath];
    }
    
    return _docInboxPath;
}

- (NSString *)libPath {
    if (!_libPath) {
        _libPath = [self domainPathWithOriginPath:libPath];
    }
    
    return _libPath;
}

- (NSString *)libPrefPath {
    if (!_libPrefPath ) {
        _libPrefPath = [self domainPathWithOriginPath:libPrefPath];
    }
    
    return _libPrefPath;
}
- (NSString *)libCachePath {
    if (!_libCachePath ) {
        _libCachePath = [self domainPathWithOriginPath:libCachePath];
    }
    
    return _libCachePath;
}

- (NSString *)tmpPath {
    if (!_tmpPath ) {
        _tmpPath = [self domainPathWithOriginPath:tmpPath];
    }
    
    return _tmpPath;
}

#pragma mark - private
- (NSString *)domainPathWithOriginPath:(NSString *)originPath {
    if (!originPath) {
        return nil;
    }
    
    if (!self.domain || !self.uniqueUserName) {
        return originPath;
    }
    
    NSString *dstPath = [originPath stringByAppendingPathComponent:self.domain];
    dstPath = [dstPath stringByAppendingPathComponent:self.uniqueUserName];
    
    [self touch:dstPath];
    return dstPath;
}

- (BOOL)touch:(NSString *)path {
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        
        return [[NSFileManager defaultManager] createDirectoryAtPath:path
                                         withIntermediateDirectories:YES
                                                          attributes:nil
                                                               error:NULL];
    }
    
    return YES;
}
@end

@implementation TYSandbox (MUPModuleSandbox)

+ (instancetype)sandboxWithDomain:(NSString *)domain uniqueUserName:(NSString *)uniqueUserName {
    TYSandbox *sandbox = [[[self class] alloc] init];
    sandbox.domain = domain;
    sandbox.uniqueUserName = uniqueUserName;
    
    return sandbox;
}

@end
